using DevExpress.XtraEditors;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using WinformDevelopFrame.Core.Extend;
using WinformDevelopFrame.Core.Form;
using WinformDevelopFrame.Entity;
using WinformDevelopFrame.IServices;
using WinformDevelopFrame.Services;

namespace  WinformDevelopFrame.UI
{
    public partial class Frmsysdatatable : BaseForm
    {
        private ISqlSugarClient _sqlSugarClient;
        private ISysDataTableService _sysdatatableService;
        private IsysconnectService _connectService;
        private IsysmenuService _sysmenuService;
        public  Frmsysdatatable(ISqlSugarClient sqlSugarClient, ISysDataTableService sysdatatableService, IsysconnectService sysConnectService,IsysmenuService sysmenuService)
        {
            _sqlSugarClient = sqlSugarClient;
            _sysdatatableService = sysdatatableService;
            _connectService = sysConnectService;
            _sysmenuService= sysmenuService;
            InitializeComponent();
            Init();
            SetReadOnlyField();
        }

        private void Frmsysdatatable_Load(object sender, EventArgs e)
        {

        }
        //设置不可编辑
        private void SetReadOnlyField()
        {
        }
        /// <summary>
        /// 设置数据 设置列名
        /// </summary>
        private void Init()
        {
            fileds.Add("DataTableName", "数据源名称");
            fileds.Add("SQL", "数据源Sql");
            fileds.Add("Remark", "备注");
            var m = _sysmenuService.QueryByClause(p => p.MenuCode.Equals(this.Name));
            //获取工具按钮
            var menus = _sysmenuService.QueryListByClause(p => p.PId == m.Id).OrderBy(p => p.OrderNo).ToList();
            menus.ForEach(p =>
            {
                btns.Add(new SimpleButton()
                {
                    Text = p.MenuText,
                    Name = p.MenuCode,
                    Image = Image.FromFile(Application.StartupPath + p.MenuIcon)
                });
            });

        }

        public override void SetGridData()
        {
            gridControl1.DataSource = _sysdatatableService.Query();
        }

        /// <summary>
        /// 数据校验
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(DataTableName.Text))
            {
                "数据源名称不能为空".ShowTips();
                return false;
            }
            if (string.IsNullOrEmpty(SQL.Text))
            {
                "数据源Sql不能为空".ShowTips();
                return false;
            }
            return true;
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            gridView2.Columns.Clear();
            gridControl2.DataSource = _sqlSugarClient.Ado.GetDataTable(SQL.Text);
        }

        public override void AddFunction()
        {
            
        }

        public override void EditFunction()
        {
            base.EditFunction();
        }

        public override bool SaveFunction()
        {
            if (CheckData())
            {

                if (FormStatus==FormStatus.Add)
                {
                    ID = _sysdatatableService.Insert(this.ControlDataToModel<sysDataTable>());
                    return ID > 0;
                }
                else
                {
                    return _sysdatatableService.UpdateN(this.ControlDataToModel<sysDataTable>());
                }
            }
            return false;
        }
        public override void GridControlDoubleClickFunction()
        {
            ID = ((sysDataTable)gridView1.GetRow(gridView1.FocusedRowHandle)).Id;
        }
        public override bool DeleteFunction()
        {
            return _sysdatatableService.DeleteById(ID);
        }

        public override bool CancelFunction()
        {
            return base.CancelFunction();
        }
    }
}